package com.minelx.statementsplus.cha.webauto;

import com.minelx.autocha.IWebBrowser;
import com.minelx.autocha.WebNode;
import com.minelx.statementsplus.cha.core.TaskCommitment;
import org.openqa.selenium.WebElement;

import static com.minelx.autocha.WebElements.attribute;

public class ChaPage implements AutoCloseable {
	public static final String URL_HEAD = "http://cha.498.net/tools/daoshuju";

	public static final String TIPS___EMPTY_RECORD_AFTER_EXECUTION = "主SQL语句查询结果为空或语句有问题,请核对~";

	private final IWebBrowser browser;

	public ChaPage(IWebBrowser browser) {
		this.browser = browser;
	}

	public String tips() {
		return browser.selector("#rlt").text();
	}

	public String executionProgress() {
		return executionProgressLabel().text();
	}

	public void explain(TaskCommitment commitment) {
		browser.id("id").value(commitment.loopId());
		majorSqlTextField().value(commitment.majorSql());
		childSqlTextField().value(commitment.childSql());
		browser.id("addi").click();
		browser.id("addiv").sendKeys(commitment.additionalValues());
		doExpress();
		// wait til it presented
		executeButton().until(WebElement::isEnabled);
	}

	public boolean explain(String majorSql) {
		majorSqlTextField().text(majorSql);
		expressButton().click();
		try {
			// expressing passed
			majorSqlTextField().until(attribute("disabled", "true"), 5);
			return true;
		} catch (IllegalStateException ignored) {
			return false;
		}
	}

	public String executeForTips() {
		String usedProgress = executionProgress();
		// start execution
		executeButton().click();
		// until progress label change
		try {
			// FIXME 2021/7/28  wait for me!!!     HEREREREREREREREREREERERERERER
			executionProgressLabel().until(webElement -> !webElement.getText().equals(usedProgress), 5);
		} catch (IllegalStateException e) {
			return tips();
		}
		// wait for completion of download
		downloadButton().until(WebElement::isEnabled);
		deleteButton().until(WebElement::isEnabled);
		return tips();
	}

	private void doExpress() {
		if (expressButton().attr("value").equals("重新解析")) {
			expressButton().click();
			expressButton().click();
		} else if (expressButton().attr("value").equals("解析主语句")) {
			expressButton().click();
		} else {
			throw new RuntimeException("operation is unsupported.");
		}
	}

	public DownloadRequest getDownloadRequest() {
		String secret = (String) browser.coreDriver().executeScript("return s");
		String sessionId = browser.coreDriver().manage().getCookies().stream()
				.filter(cookie -> cookie.getName().equals("PHPSESSID"))
				.findFirst().orElseThrow(RuntimeException::new).toString();
		return new DownloadRequest(secret, sessionId);
	}

	private WebNode childSqlTextField() {
		return browser.id("sql_sub");
	}

	private WebNode expressButton() {
		return browser.id("exp_btn");
	}

	public WebNode executeButton() {
		return browser.id("dow_btn");
	}

	public WebNode downloadButton() {
		return browser.id("get_btn");
	}

	private WebNode deleteButton() {
		return browser.id("del_btn");
	}

	private WebNode majorSqlTextField() {
		return browser.id("sql_main");
	}

	private WebNode executionProgressLabel() {
		return browser.selector("#cen_foot > div > div");
	}

	public void deleteIfExists() {
		deleteButton().ifExists(WebElement::click);
	}

	@Override
	public void close() {
		deleteIfExists();
		browser.close(500);
	}
}
